package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    UserService userService;

    /**
     * 查询用户资料
     *    参数：
     *      token，userID
     *    请求连接
     *       Get   /users
     *    返回值：
     *       UserInfo
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        //如果传递了userID参数：根据指定id查询
        //没有传递，解析token获取用户id
        User user = UserHolder.get();
        if(userID == null) {
            userID = Long.valueOf(user.getId());
        }
        //根据id查询userinfo
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo,
                                 @RequestHeader("Authorization") String token) {

        //JWT中：Long  --- Integer
        //解析token获取id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");

        //设置id
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * 参数：
     *    地址参数：type
     *          1 互相关注
     *          2 我关注
     *          3 粉丝
     *          4 谁看过我
     *    page，pagesize
     * 返回值：
     *    PageResult<UserLikeListVo>
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10")  Integer pagesize) {
        PageResult pr =  userInfoService.friends(type,page,pagesize);
        return ResponseEntity.ok(pr);
    }
    /**
     * 修改手机号
     * 接口路径：POST/users/phone/sendVerificationCode
     * 参数无
     * 返回 无
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String mobile = UserHolder.getMobile();
        userService.login(mobile);
        return ResponseEntity.ok(null);
    }
    /**
     * 修改手机号
     * 校验验证码
     * 接口路径：POST/users/phone/checkVerificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
       String verificationCode = (String) map.get("verificationCode");
        Map<String,Boolean>map1 = userService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(map1);
    }
    /**
     * 修改手机号 保存
     * 接口路径：POST/users/phone
     * 参数 新手机号	phone
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        userService.updatephone(map);
        return ResponseEntity.ok(null);
    }

    /*
    * 互相喜欢，喜欢，粉丝 - 统计
    *   GET/users/counts
    * */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map retMap = userInfoService.counts();
        return ResponseEntity.ok(retMap);
    }

    /*
    * 喜欢 - 取消
    *   DELETE/users/like/:uid
    * */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity rescindLike(@PathVariable("uid") Long uid){
        userInfoService.rescindLike(uid);
        return ResponseEntity.ok(null);
    }

    /*
    * 粉丝 - 喜欢
    *   POST/users/fans/:uid
    * */

    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") Long uid){
        userInfoService.fans(uid);
        return ResponseEntity.ok(null);
    }
}
